#!/bin/sh
#
# Inventory -- take an inventory of the lsof distribution's 00MANIFEST

# Establish trap and stty handling.

ISIG=":"
trap '$ISIG; exit 1'  1 2 3 15
stty -a 2>&1 | grep isig > /dev/null
if test $? -eq 0
then
  stty -a 2>&1 | grep -E -e -isig > /dev/null
  if test $? -eq 0
  then
    ISIG="stty -isig"
    stty isig
  fi
fi

# Establish echo type -- Berkeley or SYSV.

j=$(echo -n "")
if test "X$j" = "X-n "
then
  EC="\c"
  EO=""
else
  EC=""
  EO="-n"
fi

# Display the introduction and basic explanation.

cat << .CAT_MARK

This configuration step (the Inventory script) takes inventory of
the lsof distribution.  The script runs for a minute or two while
it checks that all the subdirectories, information files, scripts,
header files and source files that should be present really are.

It's not absolutely necessary that you take inventory, but it's a
good idea to do it right after the lsof distribution has been
unpacked.  Once the inventory has been taken, this script creates
the file ./.ck00MAN as a signal that the inventory step has been
done.

You can call the Inventory script directly at any time to take
inventory.  You can inhibit the inventory step permanently by
creating the file ./.neverInv, and you can tell the Configure script
to skip the inventory and customization steps with the -n option.
.CAT_MARK

END=0
while test $END = 0
do
  echo ""
  echo $EO "Do you want to take inventory (y|n) [y]? $EC"
  read ANS EXCESS
  if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
  then
    exit 0
  fi
  if test "X$ANS" = "Xy" -o "X$ANS" = "XY" -o "X$ANS" = "X"
  then
    END=1
  else
    echo ""
    echo "Please answer y or n."
  fi
done

# The current directory is assumed to be the lsof distribution home.

D=$(pwd)

# If .ck00MAN exists, the manifest has already been checked.
# See if the caller wants to check it again.

CK=$D/.ck00MAN
if test -r $CK
then
  cat << .CAT_MARK

======================================================================

The lsof distribution inventory in 00MANIFEST has already been checked.
.CAT_MARK

  END=0
  while test $END = 0
  do
    echo ""
    echo $EO "Do you want to check the inventory again (y|n) [n]? $EC"
    read ANS EXCESS
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN" -o "X$ANS" = "X"
    then
      exit 0
    else
      if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
      then
	END=1
      else
	echo ""
	echo "Please answer y or n."
      fi
    fi
  done
fi
echo ""

# See if manifest exists.  Exit if it does not.

if test ! -r 00MANIFEST
then
  echo "FATAL: 00MANIFEST file not found or not readable; Inventory exits."
  echo ""
  exit 1
fi

# Start the inventory.

S=""
echo "Conducting an inventory of the lsof distribution; this will take a while."
echo ""
echo $EO "Examining ${D}:$EC"
ERR=0
OK=1
for i in $(cat 00MANIFEST | sed 's/\*$//')
do
  if test "X$i" != "X"
  then
    j=$(expr $i : '\(.*\)/$')
    if test "X$j" != "X" -a "X$j" != "X0"
    then

    # Check a subdirectory reference.

      if test ! -d ${D}/${S}/$j
      then
	if test $OK = 1
	then
	  echo ""
	fi
	echo "    Subdirectory ${S}/$j is missing. ++++"
	ERR=1
	OK=0
      fi
    else
      s=$(expr $i : '\(.*\):$')
      if test "X$s" != "X" -a "X$s" != "X0"
      then

      # Process a subdirectory change.

	if test $OK -eq 1
	then
	  echo " OK"
	fi
	OK=1
	S=$s
	echo $EO "Examining $S:$EC"
	if test ! -d ${D}/$S
	then
	  echo " ERROR"
	  echo "    Subdirectory $S is missing. ++++"
	  ERR=1
	  OK=0
	fi
      else

	# Process a file reference.

	if test ! -r ${D}/${S}/$i
	then
	  if test $OK -eq 1
	  then
	    echo " ERROR"
	  fi
	  echo "    File ${S}/$i is missing. ++++"
	  ERR=1
	  OK=0
	fi
      fi
    fi
  fi
done
if test $OK -eq 1
then
  echo " OK"
fi
echo ""
if test $ERR -ne 0
then
  echo "+++++++++++++++++++++++++++++++++++++++++++++++"
  echo "+                                             +"
  echo "+  SOME FILES OR DIRECTORIES MAY BE MISSING!  +"
  echo "+                                             +"
  echo "+++++++++++++++++++++++++++++++++++++++++++++++"
else
  echo "This lsof distribution seems to be complete."
fi
echo ""
echo "" >> $CK
exit $ERR
